package com.smzd.projforge.actions;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.core.actions.AbstractScopedController;
import com.smzd.core.annotations.Access;
import com.smzd.core.business.AbstractService;
import com.smzd.core.utils.db.SRPage;
import com.smzd.core.utils.json.JsonHelper;
import com.smzd.projforge.business.DynaClassService;
import com.smzd.projforge.business.ProjectService;
import com.smzd.projforge.domain.DynaClass;
import com.smzd.projforge.domain.Project;

@Controller
@Access
@RequestMapping("/project/{scopeId}/classes")
public class DynaClassController extends
		AbstractScopedController<DynaClass, Project> {
	@Autowired
	private DynaClassService service;
	@Autowired
	private ProjectService projService;

	@Override
	protected AbstractService<DynaClass> getService() {
		return service;
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAddPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaClass elem,
			BindingResult result) throws Exception {
		return super.toAddPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String postAddData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaClass elem,
			BindingResult result) throws Exception {

		return super.postAddData(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(method = RequestMethod.GET)
	public String toListPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaClass elem,
			BindingResult result) throws Exception {
		return super.toListPage(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String getListData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId,
			SRPage<Project, DynaClass> paging, BindingResult result)
			throws Exception {
		return super.getListData(model, scopeId, paging, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String toEditPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaClass elem,
			BindingResult result) throws Exception {
		elem = service.getItemById(elem.getId());
		return super.toEditPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public String postEditData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaClass elem,
			BindingResult result) throws Exception {
		return super.postEditData(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/remove")
	@ResponseBody
	public String postRemoveData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaClass elem,
			BindingResult result) throws Exception {
		return super.postRemoveData(model, scopeId, elem, result);
	}

	@Override
	protected Project getScope(int scopeId) {
		return projService.getItemById(scopeId);
	}
	
	///////////////扩展功能--动态类多对多编辑////////////////////
	/**
	 * toMasterClassesPage：导航到当前类的主类列表配置页面。<br>
	 * @param model
	 * @param slaveId：从类Id
	 * @return
	 */
	@RequestMapping(value = "/slave-{slaveId}", method = RequestMethod.GET)
	public String toMasterClassesPage(Map<String, Object> model,
			@PathVariable("slaveId") int slaveId) throws Exception {
		DynaClass slave = service.getItemById(slaveId);
		model.put("item", slave);
		return getMyView("masters.ftl");
	}
	/**
	 * getMasterClassesData：获取所有可配置的主类列表。<br>
	 * 已经为当前类配备了多对多关系的主类被列在前面，并有字段标志<br>
	 * @param model
	 * @param scopeId
	 * @param slaveId
	 * @param page
	 * @param result
	 * @return
	 */
	@RequestMapping(value = "/list/slave-{slaveId}", method = RequestMethod.POST)
	@ResponseBody
	public String getMasterClassesData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, 
			@PathVariable("slaveId") int slaveId, 
			SRPage<Project, DynaClass> page,
			BindingResult result) throws Exception {
		page.setExt(String.valueOf(slaveId));
		page.setScope(new Project(scopeId));
		return service.getMasterClassesData(page);
	}

	/**
	 * saveGroupRoles：保存以当前类为从类的多对多关系的改变情况。<br>
	 * 只有新改变的部分数据需要提交服务器端进行处理。<br>
	 * @param model
	 * @param scopeId
	 * @param slaveId
	 * @param masterIds
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/edit/slave-{slaveId}", method = RequestMethod.POST)
	@ResponseBody
	public String saveGroupRoles(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, 
			@PathVariable("slaveId") int slaveId, 
			@RequestParam("masterIds") String masterIds)
			throws Exception {
		if (StringUtils.isEmpty(masterIds)) {
			return JsonHelper.getSuccessJsonString("没有任何变动提交！");
		}
		service.saveMMRelations(scopeId, slaveId, masterIds);
		return JsonHelper.getSuccessJsonString("保存成功！");
	}
	
	@RequestMapping(value="/mtmcandidates")
	@ResponseBody
	public String getMTMRelationClassCandidates(@PathVariable("scopeId") int scopeId){
		return service.getMTMRelationClassCandidates(scopeId);
	}
	
//	@RequestMapping("/generate")
//	public String startGenerateCodeTest(Integer id){
//		service.generateClassCodeTest(id);
//		return null;
//	}

}
